package middleSearch;

/**
 * @description: 描述
 * @author: Mr.Hu
 * @create: 2022/3/19 10:43
 **/
public class FindBadVersion {

    public static void main(String[] args) {
        FindBadVersion findBadVersion = new FindBadVersion();
        int badVersion = findBadVersion.findBadVersion(5);
        System.out.println(badVersion);
    }

    public static int findBadVersion(int length) {
        int head = 1;
        int tail = length;
        int middle;
        while (head <= tail) {
            middle = (tail-head)/2+head;
            if((!isBadVersion(middle-1) && isBadVersion(middle)||(isBadVersion(middle) && middle >= length))){
                return middle;
            }else if (!isBadVersion(middle) && !isBadVersion(middle-1)){
                head = middle+1;
            }else if (isBadVersion(middle)  && isBadVersion(middle-1)){
                tail = middle-1;
            }
        }
        return -1;
    }
     public static boolean isBadVersion(int version){
     if(version >= 4){
         return true;
     }else {
         return false;
     }
    }

}
